//
//  HWVipGiftBagCouponsCell.swift
//  HWanGame
//
//  Created by 王昌健 on 2021/12/14.
//  Copyright © 2021 94Wan. All rights reserved.
//

import UIKit

class HWVipGiftBagCouponsCell: UITableViewCell {
    var bgImageView:UIImageView!
    var priceLabel:UILabel!
    var priceBottomLabel:UILabel!
    var nameLabel:UILabel!
    var content:UILabel!
    var timeLabel:UILabel!
    var receiveButton:UIButton!
    
    var receiveButtonClick:((_:String)->())?
    
    var model:HWCouponsListModel? {
        didSet {
            priceLabel.text = model?.jian
            priceBottomLabel.text = model?.money
            nameLabel.text = model?.name
            content.text = model?.gname
            timeLabel.text = model?.end_time
            
            let attriString = NSMutableAttributedString.init(string: "￥" + (priceLabel.text ?? ""))
            
            attriString.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: 12*widthScale), range: NSMakeRange(0, 1))
            attriString.addAttribute(NSAttributedString.Key.font, value: UIFont.boldSystemFont(ofSize: 18*widthScale), range: NSMakeRange(1, attriString.length-1))
            
            priceLabel.attributedText = attriString
            receiveButton.isEnabled = true
            
            if model?.status == 0 {
                receiveButton.setTitle("领取", for: .normal)
                receiveButton.setTitleColor(UIColor.colorWithHexString(hexString: "#4F3200"), for: .normal)
                receiveButton.setBackgroundImage(UIImage(named: "wearButton"), for: .normal)
                bgImageView.image = UIImage(named: "receiveCouponsBg")
            }else if model?.status == 1 {
                receiveButton.setTitle("已领取", for: .normal)
                receiveButton.setTitleColor(UIColor.colorWithHexString(hexString: "#666666"), for: .normal)
                receiveButton.setBackgroundImage(UIImage(named: "receiveButtonGray"), for: .normal)
                bgImageView.image = UIImage(named: "receivedCouponsBg")
            }else if model?.status == 2 {
                receiveButton.setTitle("领取", for: .normal)
                receiveButton.setTitleColor(UIColor.colorWithHexString(hexString: "#666666"), for: .normal)
                receiveButton.setBackgroundImage(UIImage(named: "receiveButtonGray"), for: .normal)
                receiveButton.isEnabled = false
                bgImageView.image = UIImage(named: "receiveCouponsBg")
                
            }
        }
    }
    
    class func cellWithTableView(tableView:UITableView) -> (HWVipGiftBagCouponsCell) {
        let reUse:String = "HWVipGiftBagCouponsCell"
        
        var cell = tableView.dequeueReusableCell(withIdentifier: reUse)
        
        if cell == nil {
            cell = HWVipGiftBagCouponsCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: reUse)
        }
        
        return cell as! HWVipGiftBagCouponsCell
    }
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        initUI()
        initFrame()
    }
    
    override var frame: CGRect {
        get {
            return super.frame
        }
        set {
            var frame = newValue
            frame.origin.x += 15
            frame.size.width -= 2 * 15
            super.frame = frame
        }
    }
    
    func initUI() -> Void {
        selectionStyle = UITableViewCell.SelectionStyle.none
        backgroundColor = UIColor.clear
        
        bgImageView = UIImageView()
        bgImageView.image = UIImage(named: "receiveCouponsBg")
        bgImageView.isUserInteractionEnabled = true
        contentView.addSubview(bgImageView)
        
        priceLabel = HWLabel.createBoldLabel(title: "", color: "#FFFFFF", font: 18*widthScale)
        priceLabel.backgroundColor = .clear
        priceLabel.textAlignment = .center
        bgImageView.addSubview(priceLabel)
        
        priceBottomLabel = HWLabel.createLabel(title: "", color: "#FFFFFF", font: 10*widthScale)
        priceBottomLabel.backgroundColor = .clear
        priceBottomLabel.textAlignment = .center
        bgImageView.addSubview(priceBottomLabel)
        
        nameLabel = HWLabel.createBoldLabel(title: "", color: "#333333", font: 12*widthScale)
        bgImageView.addSubview(nameLabel)
        
        content = HWLabel.createLabel(title: "", color: "#999999", font: 10*widthScale)
        bgImageView.addSubview(content)
        
        timeLabel = HWLabel.createLabel(title: "", color: "#999999", font: 10*widthScale)
        bgImageView.addSubview(timeLabel)
        
        receiveButton = UIButton(type: .custom)
        receiveButton.setTitle("领取", for: .normal)
        receiveButton.titleLabel?.font = .boldSystemFont(ofSize: 12*widthScale)
        receiveButton.setTitleColor(.colorWithHexString(hexString: "#4F3200"), for: .normal)
        receiveButton.setBackgroundImage(UIImage(named: "wearButton"), for: .normal)
        receiveButton.addTarget(self, action: #selector(receiveButtonClick(_:)), for: .touchUpInside)
        bgImageView.addSubview(receiveButton)
    }
    
    func initFrame() -> Void {
        bgImageView.snp.makeConstraints { make in
            make.left.right.top.bottom.equalToSuperview()
            make.height.equalTo(83*widthScale)
        }
        
        priceLabel.snp.makeConstraints { make in
            make.left.equalTo(5*widthScale)
            make.width.equalTo(bgImageView.snp.width).multipliedBy(0.23)
            make.top.equalTo(18*widthScale)
        }
        
        priceBottomLabel.snp.makeConstraints { make in
            make.left.equalTo(5*widthScale)
            make.width.equalTo(bgImageView.snp.width).multipliedBy(0.23)
            make.top.equalTo(priceLabel.snp.bottom).offset(5*widthScale)
        }
        
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(priceLabel.snp.right).offset(15*widthScale)
            make.top.equalTo(15*widthScale)
        }
        
        content.snp.makeConstraints { make in
            make.left.equalTo(priceLabel.snp.right).offset(15*widthScale)
            make.top.equalTo(nameLabel.snp.bottom).offset(6*widthScale)
        }
        
        timeLabel.snp.makeConstraints { make in
            make.left.equalTo(priceLabel.snp.right).offset(15*widthScale)
            make.top.equalTo(content.snp.bottom).offset(6*widthScale)
        }
        
        receiveButton.snp.makeConstraints { make in
            make.right.equalTo(-24*widthScale)
//            make.top.equalTo(13*widthScale)
            make.centerY.equalToSuperview()
            make.width.equalTo(56*widthScale)
            make.height.equalTo(27*widthScale)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

extension HWVipGiftBagCouponsCell {
    @objc func receiveButtonClick(_ button:UIButton) -> Void {
        self.receiveButtonClick?(model?.id ?? "")
    }
}
